package com.example.blog.common.Utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtil {
    //对密码进行加密
    public  String encrypt(String password){
        //生成盐值   replace方法替换字符
        String salt = UUID.randomUUID().toString().replace("-","");
        String finalpassword= DigestUtils.md5DigestAsHex((salt+password).getBytes(StandardCharsets.UTF_8));
        return salt+finalpassword;
    }

    //检验密码
    public  Boolean verify(String password,String finalpassword){
        if(!StringUtils.hasLength(password)&&!StringUtils.hasLength(finalpassword)){
            return false;
        }
        if(finalpassword.length()!=64){
            return false;
        }
        String salt=finalpassword.substring(0,32);
        String securitypasswrod=DigestUtils.md5DigestAsHex((salt+password).getBytes(StandardCharsets.UTF_8));
        return finalpassword.equals(securitypasswrod);
    }
}
